local s15__canpi = fk.CreateSkill {
  name = "s15__canpi",
}

Fk:loadTranslationTable {
  ["s15__canpi"] = "叅毗",
  [":s15__canpi"] = "前锋技,结束阶段或己方主帅进入濒死状态时,你可以弃置两张牌或失去1点体力,令己方主帅选择一项:1.回复1点体力并复原武将牌;2.摸两张牌。",

  ["#s15__canpi"] = "是否发动叅毗",
  ["#s15__canpi-invoke"] = "叅毗：弃两张牌或失去1点体力（点取消），令己方主帅选择一项:1.回复1点体力并复原武将牌;2.摸两张牌。",
}

local spec = {
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = event:getCostData(self).cards
    if #card == 2 then
      room:throwCard(event:getCostData(self).cards, s15__canpi.name, player, player)
    else
      room:loseHp(player, 1, s15__canpi.name)
    end

    -- 通过座次判定己方主帅
    local commander = nil
    for _, p in ipairs(room.players) do
      if p:isFriend(player) and (p.seat == 2 or p.seat == 5) then
        commander = p
        break
      end
    end

    if not commander then return end

    if not commander.dead and not commander:isWounded() then
      commander:drawCards(2, s15__canpi.name)
    elseif not commander.dead and commander:isWounded() then
      local choices = { "draw2", "recover" }
      local choice = room:askToChoice(commander, {
        choices = choices,
        skill_name = s15__canpi.name,
      })
      if choice == "draw2" then
        commander:drawCards(2, s15__canpi.name)
      elseif choice == "recover" then
        room:recover {
          who = commander,
          num = 1,
          recoverBy = player,
          skillName = s15__canpi.name,
        }
        commander:reset()
      end
    end
  end,
}
s15__canpi:addEffect(fk.AskForPeaches, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(s15__canpi.name) and
        data.who:isFriend(player) and (data.who.seat == 2 or data.who.seat == 5)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 找到己方主帅用于显示prompt
    local commander = nil
    for _, p in ipairs(room.players) do
      if p:isFriend(player) and (p.seat == 2 or p.seat == 5) then
        commander = p
        break
      end
    end

    local invoke = room:askToSkillInvoke(player, { skill_name = s15__canpi.name, prompt = "#s15__canpi" })
    if not invoke then return false end

    local card = room:askToDiscard(player, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = s15__canpi.name,
      cancelable = true,
      prompt = "#s15__canpi-invoke",
      skip = true,
    })
    event:setCostData(self, { tos = { commander or data.who }, cards = card })
    return true
  end,
  on_use = spec.on_use,
})
s15__canpi:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(s15__canpi.name) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 找到己方主帅用于显示prompt和设置目标
    local commander = nil
    for _, p in ipairs(room.players) do
      if p:isFriend(player) and (p.seat == 2 or p.seat == 5) then
        commander = p
        break
      end
    end

    local invoke = room:askToSkillInvoke(player, { skill_name = s15__canpi.name, prompt = "#s15__canpi" })
    if not invoke then return false end

    local card = room:askToDiscard(player, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = s15__canpi.name,
      cancelable = true,
      prompt = "#s15__canpi-invoke",
      skip = true,
    })
    event:setCostData(self, { tos = { commander }, cards = card })
    return true
  end,
  on_use = spec.on_use,
})
return s15__canpi
